//-----------------------------------------------------------------
// Space Out Application
// C++ Source - WARTANK.cpp
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "Wartank.h"

//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
  // Create the game engine
  g_pGame = new GameEngine(hInstance, TEXT("Wartank"),
    TEXT("Wartank"), IDI_WARTANK, IDI_WARTANK_SM, 600, 450);
  if (g_pGame == NULL)
    return FALSE;

  // Set the frame rate
  g_pGame->SetFrameRate(30);

  // Store the instance handle
  g_hInstance = hInstance;

  return TRUE;
}

void GameStart(HWND hWindow)
{
  // Seed the random number generator
  srand(GetTickCount());

  // Create the offscreen device context and bitmap
  g_hOffscreenDC = CreateCompatibleDC(GetDC(hWindow));
  g_hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow),
    g_pGame->GetWidth(), g_pGame->GetHeight());
  SelectObject(g_hOffscreenDC, g_hOffscreenBitmap);

  // Create and load the bitmaps
  HDC hDC = GetDC(hWindow);
  g_pDesertBitmap = new Bitmap(hDC, IDB_DESERT, g_hInstance);
  g_pCarBitmap = new Bitmap(hDC, IDB_CAR, g_hInstance);
  g_pSmCarBitmap = new Bitmap(hDC, IDB_SMCAR, g_hInstance);
  g_pMissileBitmap = new Bitmap(hDC, IDB_MISSILE, g_hInstance);
  g_pBlobboBitmap = new Bitmap(hDC, IDB_BLOBBO, g_hInstance);
  g_pBMissileBitmap = new Bitmap(hDC, IDB_BMISSILE, g_hInstance);
  g_pJellyBitmap = new Bitmap(hDC, IDB_JELLY, g_hInstance);
  g_pJMissileBitmap = new Bitmap(hDC, IDB_JMISSILE, g_hInstance);
  g_pTimmyBitmap = new Bitmap(hDC, IDB_TIMMY, g_hInstance);
  g_pTMissileBitmap = new Bitmap(hDC, IDB_TMISSILE, g_hInstance);
  g_pSmExplosionBitmap = new Bitmap(hDC, IDB_SMEXPLOSION, g_hInstance);
  g_pLgExplosionBitmap = new Bitmap(hDC, IDB_LGEXPLOSION, g_hInstance);
  g_pGameOverBitmap = new Bitmap(hDC, IDB_GAMEOVER, g_hInstance);
  g_pGameOver2Bitmap = new Bitmap(hDC, IDB_GAMEOVER2, g_hInstance);
  g_pTankBitmap = new Bitmap(hDC, IDB_TANK, g_hInstance);
  g_pTank2Bitmap = new Bitmap(hDC, IDB_TANK2, g_hInstance);
  g_pBuildingBitmap = new Bitmap(hDC, IDB_BUILDING, g_hInstance);

  g_J1Loose=false;
  g_J2Loose=false;

  // Create the starry background
  g_pBackground = new StarryBackground(600, 450);

  // Play the background music
  g_pGame->PlayMIDISong(TEXT("Music.mid"));

  // Start the game
  NewGame();
}

void GameEnd()
{
  // Close the MIDI player for the background music
  g_pGame->CloseMIDIPlayer();

  // Cleanup the offscreen device context and bitmap
  DeleteObject(g_hOffscreenBitmap);
  DeleteDC(g_hOffscreenDC);  

  // Cleanup the bitmaps
  delete g_pDesertBitmap;
  delete g_pCarBitmap;
  delete g_pSmCarBitmap;
  delete g_pMissileBitmap;
  delete g_pBlobboBitmap;
  delete g_pBMissileBitmap;
  delete g_pJellyBitmap;
  delete g_pJMissileBitmap;
  delete g_pTimmyBitmap;
  delete g_pTMissileBitmap;
  delete g_pSmExplosionBitmap;
  delete g_pLgExplosionBitmap;
  delete g_pGameOverBitmap;
  delete g_pGameOver2Bitmap;
  delete g_pTankBitmap;
  delete g_pTank2Bitmap;
  delete g_pBuildingBitmap;

  // Cleanup the background
  delete g_pBackground;

  // Cleanup the sprites
  g_pGame->CleanupSprites();

  // Cleanup the game engine
  delete g_pGame;
}

void GameActivate(HWND hWindow)
{
  // Resume the background music
  g_pGame->PlayMIDISong(TEXT(""), FALSE);
}

void GameDeactivate(HWND hWindow)
{
  // Pause the background music
  g_pGame->PauseMIDISong();
}

void GamePaint(HDC hDC)
{
  // Draw the background
  g_pBackground->Draw(hDC);

  // Draw the desert bitmap
  g_pDesertBitmap->Draw(hDC, 0, 371);

  // Draw the sprites
  g_pGame->DrawSprites(hDC);

  // Draw the angle J1
  
  TCHAR szText1[64];
  RECT  rect1 = { 460, 0, 510, 30 };
  wsprintf(szText1, "%d", g_iAngle);
  SetBkMode(hDC, TRANSPARENT);
  SetTextColor(hDC, RGB(255, 255, 255));
  DrawText(hDC, szText1, -1, &rect1, DT_SINGLELINE | DT_LEFT | DT_VCENTER);

  // Draw the angle J2
  
  TCHAR szText2[64];
  RECT  rect2 = { 140, 0, 190, 30 };
  wsprintf(szText2, "%d", g_iAngle2);
  SetBkMode(hDC, TRANSPARENT);
  SetTextColor(hDC, RGB(255, 255, 255));
  DrawText(hDC, szText2, -1, &rect2, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);

  // Draw the game over message, if necessary
  if (g_bGameOver)
	  if (g_J1Loose )
	  {
		   g_pGameOverBitmap->Draw(hDC, 190, 149, TRUE);
	  }
	  if (g_J2Loose )
	  {
		  g_pGameOver2Bitmap->Draw(hDC, 190, 149, TRUE);
	  }
}

void GameCycle()
{
  if (!g_bGameOver)
  {
    // Randomly add aliens
    //if ((rand() % g_iDifficulty) == 0)
      //AddAlien();

    // Update the background
    g_pBackground->Update();

    // Update the sprites
    g_pGame->UpdateSprites();

    // Obtain a device context for repainting the game
    HWND  hWindow = g_pGame->GetWindow();
    HDC   hDC = GetDC(hWindow);

    // Paint the game to the offscreen device context
    GamePaint(g_hOffscreenDC);

    // Blit the offscreen bitmap to the game screen
    BitBlt(hDC, 0, 0, g_pGame->GetWidth(), g_pGame->GetHeight(),
      g_hOffscreenDC, 0, 0, SRCCOPY);

    // Cleanup
    ReleaseDC(hWindow, hDC);
  }
}

void HandleKeys()
{
  if (!g_bGameOver)
  {
	

    POINT ptVelocity = g_pTankSprite->GetVelocity();
	POINT ptVelocity2 = g_pTankSprite->GetVelocity();
	RECT  rcBounds = { 0, 0, 600, 450 };
	RECT  rcPos = g_pTankSprite->GetPosition();
	RECT  rcPos2 = g_pTankSprite2->GetPosition();

	//-------------------------------------------------------------------------------
	// J1
    // Move the tank based upon touche 4 et 6 pav num key presses

	if (GetAsyncKeyState(VK_NUMPAD4) < 0 && ( rcPos.left > 350 ))
    {
      // Move left
      ptVelocity.x = -4;
    }
    else if (GetAsyncKeyState(VK_NUMPAD6) < 0 && ( rcPos.right < 600 ))
    {
      // Move right
      ptVelocity.x = 4;
    }
	else
    {
      // No Move
      ptVelocity.x = 0;
    }
	g_pTankSprite->SetVelocity(ptVelocity);

	//change angle J1
	if (GetAsyncKeyState(VK_NUMPAD8))
    {
		g_iAngle +=1;
		g_iAngle = max(g_iAngle, 45);
		g_iAngle = min(g_iAngle, 80);
	}
	if (GetAsyncKeyState(VK_NUMPAD5))
    {
		g_iAngle -=1;
		g_iAngle = max(g_iAngle, 45);
		g_iAngle = min(g_iAngle, 80);
	}

	// Move the tank 2 based upon left/right key presses
    if (GetAsyncKeyState(VK_LEFT) < 0 && ( rcPos2.left > 0 ))
    {
      // Move left
      ptVelocity2.x = -4;
    }
    else if (GetAsyncKeyState(VK_RIGHT) < 0 && ( rcPos2.right < 250 ))
    {
      // Move right
      ptVelocity2.x = 4;
    }
	else
    {
      // No Move
      ptVelocity2.x = 0;
    }
	g_pTankSprite2->SetVelocity(ptVelocity2);

	//change angle J1
	if (GetAsyncKeyState(VK_UP))
    {
		g_iAngle2 +=1;
		g_iAngle2 = max(g_iAngle2, 45);
		g_iAngle2 = min(g_iAngle2, 80);
	}
	if (GetAsyncKeyState(VK_DOWN))
    {
		g_iAngle2 -=1;
		g_iAngle2 = max(g_iAngle2, 45);
		g_iAngle2 = min(g_iAngle2, 80);
	}

	//-------------------------------------------------------------------------------------

    // Fire missiles based upon enter presses
    if ((++g_iFireInputDelay > 25) && GetAsyncKeyState(VK_RETURN) < 0)
    {
      // Create a new missile sprite
      //Sprite* pSprite = new Sprite(g_pMissileBitmap, rcBounds, BA_DIE);
	  Sprite* pSprite = new Sprite(g_pMissileBitmap, rcBounds, BA_DIE, g_iAngle, false);
      pSprite->SetPosition(rcPos.left + 15, 400);

      pSprite->SetVelocity(-3, -1);
      g_pGame->AddSprite(pSprite);

      // Play the missile (fire) sound
      PlaySound((LPCSTR)IDW_MISSILE, g_hInstance, SND_ASYNC |
        SND_RESOURCE | SND_NOSTOP);

      // Reset the input delay
      g_iFireInputDelay = 0;
    }

  //--------------------------------------------------------------------------------


  // Fire missiles based upon MiddleButton presses
    if ((++g_iFireInputDelay > 25) && GetAsyncKeyState(VK_SPACE) < 0)
    {
      // Create a new missile sprite
      //Sprite* pSprite2 = new Sprite(g_pMissileBitmap, rcBounds, BA_DIE);
	  Sprite* pSprite2 = new Sprite(g_pMissileBitmap, rcBounds, BA_DIE, g_iAngle2, true);
      pSprite2->SetPosition(rcPos2.left + 15, 400);
      pSprite2->SetVelocity(3, -1);
      g_pGame->AddSprite(pSprite2);

      // Play the missile (fire) sound
      PlaySound((LPCSTR)IDW_MISSILE, g_hInstance, SND_ASYNC |
        SND_RESOURCE | SND_NOSTOP);

      // Reset the input delay
      g_iFireInputDelay = 0;
    }
  }

  // Start a new game based upon an Enter (Return) key press
  if (g_bGameOver && (GetAsyncKeyState(VK_RETURN) < 0))
    // Start a new game
    NewGame();
}

void MouseButtonDown(int x, int y, BOOL bLeft)
{
}

void MouseButtonUp(int x, int y, BOOL bLeft)
{
}

void MouseMove(int x, int y)
{
}

void HandleJoystick(JOYSTATE jsJoystickState)
{
}

BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee)
{
  // See if a player missile and an Building have collided
  Bitmap* pHitter = pSpriteHitter->GetBitmap();
  Bitmap* pHittee = pSpriteHittee->GetBitmap();
  if (pHitter == g_pMissileBitmap && pHittee == g_pBuildingBitmap )
  {
    // Play the small explosion sound
    PlaySound((LPCSTR)IDW_LGEXPLODE, g_hInstance, SND_ASYNC |
      SND_RESOURCE);

    // Kill Missile
    pSpriteHitter->Kill();

    // Create a large explosion sprite at the missile position
    RECT rcBounds = { 0, 0, 600, 450 };
    RECT rcPos;
    if (pHitter == g_pMissileBitmap)
      rcPos = pSpriteHitter->GetPosition();
    else
      rcPos = pSpriteHitter->GetPosition();
    Sprite* pSprite = new Sprite(g_pLgExplosionBitmap, rcBounds);
    pSprite->SetNumFrames(8, TRUE);
    pSprite->SetPosition(rcPos.left, rcPos.top);
    g_pGame->AddSprite(pSprite);
  }

  // See if a missile has collided with the tank1
  if (pHitter == g_pTankBitmap && pHittee == g_pMissileBitmap)
  {
    // Play the large explosion sound
    PlaySound((LPCSTR)IDW_LGEXPLODE, g_hInstance, SND_ASYNC |
      SND_RESOURCE);

    // Kill the missile sprite
      pSpriteHittee->Kill();
	  pSpriteHitter->Kill();

    // Create a large explosion sprite at the tank position
    RECT rcBounds = { 0, 0, 600, 480 };
    RECT rcPos;
	rcPos = pSpriteHitter->GetPosition();
    Sprite* pSprite = new Sprite(g_pLgExplosionBitmap, rcBounds);
    pSprite->SetNumFrames(8, TRUE);
    pSprite->SetPosition(rcPos.left, rcPos.top);
    g_pGame->AddSprite(pSprite);

    g_bGameOver = TRUE;
	g_J2Loose = true;

  }

  // See if a missile has collided with the tank2
  if (pHitter == g_pTank2Bitmap && pHittee == g_pMissileBitmap)
  {
    // Play the large explosion sound
    PlaySound((LPCSTR)IDW_LGEXPLODE, g_hInstance, SND_ASYNC |
      SND_RESOURCE);

    // Kill the missile sprite
      pSpriteHittee->Kill();
	  pSpriteHitter->Kill();

    // Create a large explosion sprite at the tank position
    RECT rcBounds = { 0, 0, 600, 480 };
    RECT rcPos;
	rcPos = pSpriteHitter->GetPosition();
    Sprite* pSprite = new Sprite(g_pLgExplosionBitmap, rcBounds);
    pSprite->SetNumFrames(8, TRUE);
    pSprite->SetPosition(rcPos.left, rcPos.top);
    g_pGame->AddSprite(pSprite);

    g_bGameOver = TRUE;
	g_J1Loose = true;

  }

  return FALSE;
}

void SpriteDying(Sprite* pSpriteDying)
{
  // See if an alien missile sprite is dying
  if (pSpriteDying->GetBitmap() == g_pBMissileBitmap ||
    pSpriteDying->GetBitmap() == g_pJMissileBitmap ||
    pSpriteDying->GetBitmap() == g_pTMissileBitmap)
  {
    // Play the small explosion sound
    PlaySound((LPCSTR)IDW_SMEXPLODE, g_hInstance, SND_ASYNC |
      SND_RESOURCE | SND_NOSTOP);

    // Create a small explosion sprite at the missile's position
    RECT rcBounds = { 0, 0, 600, 450 };
    RECT rcPos = pSpriteDying->GetPosition();
    Sprite* pSprite = new Sprite(g_pSmExplosionBitmap, rcBounds);
    pSprite->SetNumFrames(8, TRUE);
    pSprite->SetPosition(rcPos.left, rcPos.top);
    g_pGame->AddSprite(pSprite);
  }
}

//-----------------------------------------------------------------
// Functions
//-----------------------------------------------------------------
void NewGame()
{
  // Clear the sprites
  g_pGame->CleanupSprites();

  RECT rcBounds = { 0, 0, 600, 450 };

  // Create building
  g_pBuildingSprite = new Sprite(g_pBuildingBitmap, rcBounds, BA_WRAP);
  g_pBuildingSprite->SetPosition(245, 250);
  g_pGame->AddSprite(g_pBuildingSprite);
  

  // Create the tank sprite j1
  g_pTankSprite = new Sprite(g_pTankBitmap, rcBounds, BA_WRAP);
  g_pTankSprite->SetPosition(450, 405);
  g_pGame->AddSprite(g_pTankSprite);

  // Create the tank sprite j2
  g_pTankSprite2 = new Sprite(g_pTank2Bitmap, rcBounds, BA_WRAP);
  g_pTankSprite2->SetPosition(150, 405);
  g_pGame->AddSprite(g_pTankSprite2);

  // Initialize the game variables
  g_iFireInputDelay = 0;
  g_iAngle = 60;
  g_iAngle2 = 60;
  g_bGameOver = FALSE;

  // Play the background music
  g_pGame->PlayMIDISong();
}

